// pages/register/register.js

import { isRegister } from '../../api/tbPersonal'

var QQMapWX = require('../../libs/qqmap-wx-jssdk.js');
var qqmapsdk;

Page({

  /**
   * 页面的初始数据
   */
  data: {
    companylist:[],
    passnum:["是","否"],
    healthylist:["正常","发热，咳嗽等"],
    vehiclelist: ["飞机", "高铁", "火车", "自驾", "其他"],
    companyidlist:["j","a","b"],
    companyid:"",
    companynamelist:["a","b","c"],
    companyname:"",
    qiyename:'',
    phonenum:"",
    name:"",
    address:"",
    date:"",
    ispass: "",
    ispassname:"",
    healthy:"",
    vehicle:"",
    peopleid:"",
    wxid:"",
    openid:"",
    phone:'',
    items: [
      { name: '访客', value: '4',checked:true},
      { name: '员工', value: '1'},
    ],
    param5:'4',//访客or员工

    param6:'',//longitude
    param7:'',//latitude
    param8:'',//address

    isYg:false,
    // longitude: '',
    // latitude: '',
    // address:"",

  },
  radioChange: function (e) {
    console.log('radio发生change事件，携带value值为：', e.detail.value)
    this.setData({param5:e.detail.value})
  },

  
  usercompanyidInput(e){
    this.setData({
      companyid: e.detail.value
    })
  },

  usercompanynameInput(e) {
    this.setData({
      qiyename:e.detail.value,
      companyname: e.detail.value
    })
  },
  userwxidInput(e) {
    this.setData({
      wxid: e.detail.value
    })
  },
  userphonenumInput(e) {
    this.setData({
      phonenum: e.detail.value
    })
  },

  usernameInput(e) {
    this.setData({
      name: e.detail.value
    })
  },
  useraddressInput(e) {
    this.setData({
      address: e.detail.value
    })
  },
  usercpeopleidInput(e) {
    this.setData({
      peopleid: e.detail.value
    })
  },
  changeDate(e) {
    this.setData({ date: e.detail.value });
  },
  changeIspass(e) {
    this.setData({ ispass: e.detail.value, ispassname: e.detail.value === "0" ? "是" : "否", });
  },

  changehealthy(e) {
    this.setData({ healthy: this.data.healthylist[parseInt(e.detail.value)] });
  },
  changevehicle(e) {
    this.setData({ vehicle: this.data.vehiclelist[parseInt(e.detail.value)] });
  },
  changecompanyid(e) {
    this.setData({ companyid: this.data.companyidlist[parseInt(e.detail.value)] });
  },
  changecompanyname(e) {
    this.setData({ companyname: this.data.companynamelist[parseInt(e.detail.value)] });
  },
//
  // 注册
  // isRegister() {
  //   let data = {
  //     param2: this.data.openid||'ohnrI5SaDXbFBQaDW-FDvg0_1B2A'
  //   }
  //   isRegister(data).then(res => {
  //     console.log(res)
  //       if(res1.isRegister === "1"){
  //         wx.switchTab({
  //           url: '../main/main'
  //         })
  //       }
  //   })
  // },

//
  register(e){
    this.setData({
      phonenum: wx.getStorageSync('phone')
    })
    wx.removeStorageSync('companyName')
    wx.setStorageSync('isRegister',true)
    
    if(this.data.param5 =='4'){
      this.setData({
        companyname:''
      })
    }
  
    if (this.data.name != '' && this.data.peopleid != '' && this.data.param5 != ''){
      var that = this
      wx.request({
        url: 'https://shouqian.lianchuanghj.com/wxapi/tbPersonal/register', //仅为示例，并非真实的接口地址
        data: {
          "address": that.data.address,
          "backTime": that.data.date,
          "companyCode": that.data.companyid,
          "companyName": that.data.companyname,
          "health": that.data.healthy,
          "isPassAffected": that.data.ispass,
          "name": that.data.name,
          "param1": that.data.peopleid,
          "telephone": that.data.phonenum,
          "vehicle": that.data.vehicle,
          "param3": that.data.wxid,
          "param2": that.data.openid,
          "param5": that.data.param5,
          "param6": that.data.param6,
          "param7": that.data.param7,
          "param8": that.data.param8 //address
        },
        method: 'post',
        // header: {
        //   'content-type': 'application/json' // 默认值
        // },
        success(res) {
          console.log(res.data)
          // if(!res.data.data.isNglRegister){
          //   wx.showToast({
          //     title: '网络异常',
          //     icon:'none'
          //   })
          //   return false
          // }
          if(res.data.data.isRegister =='0'){
            wx.showToast({
              title: '注册失败',
              icon:'none'
            })
            return false
          }
          wx.removeStorageSync('companyName')
          wx.setStorageSync('isRegister',true)
         
          if( that.data.param5 =='4'){
            wx.setStorageSync('roleType','4')
            wx.redirectTo({
              url: '/pages/ngl/ngl'
            })
          }else{
            console.log(res)
            wx.setStorageSync('roleCode',res.data.data.tbRolePersonalRel.roleCode)
            wx.setStorageSync('personalCode',res.data.data.tbRolePersonalRel.personalCode)
            
            wx.setStorageSync('roleType',res.data.data.isRegister)
            // wx.setStorageSync('roleCode',1)

            // wx.setStorageSync('roleType',3)


            wx.reLaunch({
              url: '/pages/ngl/ngl'
            })
          }
          // wx.switchTab({
          //   url: '../main/main'
          // })
        },
        fail(res) {
          console.log(res)
        }
      })
    }else{
      wx.showToast({
        title: '请全部填写！',
        icon:'none'
      })
    }
    
    
  },


  canleZc(e) {
    console.log(e)
    wx.removeStorageSync('companyId')
    wx.removeStorageSync('companyName')
    wx.removeStorageSync('companyLocation')

    wx.reLaunch({
      url: '/pages/login/login'
    })
  },




  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var that = this
  
    if(wx.getStorageSync('companyName')){
      this.setData({
        companyname: wx.getStorageSync('companyName'),
        qiyename: wx.getStorageSync('companyName'),
        param5:'1',
        items: [
          { name: '访客', value: '4'},
          { name: '员工', value: '1',checked:true},
        ],
        isYg:true
      })
    }
    if(wx.getStorageSync('companyLocation')){
      this.setData({ param8:wx.getStorageSync('companyLocation') });
    }
    if(wx.getStorageSync('companyId')){
      this.setData({ companyid:wx.getStorageSync('companyId') });
    }


    // wx.request({
    //   url: 'https://shouqian.lianchuanghj.com/wxapi/tlCompany/getTlCompanys', //仅为示例，并非真实的接口地址
    //   data: {
    //     tlCompany: {}
    //   },
    //   method: 'post',
    //   success(res) {
    //     if(res.data.code === "00000"){
    //       // this.data.companylist = res.data.data
    //       var cid = []
    //       var cname = []
    //       res.data.data.forEach((e,i) => {
    //         cid.push(e.companyCode)
    //         cname.push(e.companyName)
    //       })
    //       that.setData({
    //         companyidlist: cid,
    //         companynamelist: cname
    //       })
    //     }
    //   }
    // })



    qqmapsdk = new QQMapWX({
      key: '7AHBZ-WSSK3-3IB32-YR6ZX-2MMNE-LUBVJ'
    });


    

    var _this = this;
    wx.getLocation({
      success: res => {
        
        _this.setData({
          param6: res.longitude,
          param7: res.latitude
        })
        
        qqmapsdk.reverseGeocoder({
          location: {
            longitude: res.longitude,
            latitude: res.latitude
          },
          get_poi: 1,
          poi_options: 'policy=2;radius=3000;page_size=20;page_index=1',
          success: function (addres) {
            _this.setData({
              param8:addres.result.address
            })

          },
          fail: function (res) {
            console.log(res);
          },
          complete: function (res) {
            console.log(res);
          }
        });
        
      },
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})